//
//  KnowMoreVC.m
//  NewPro
//
//  Created by 何荧红 on 2019/1/15.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "KnowMoreVC.h"
#import "KnowMoreModel.h"
#import "KnowMoreFCell.h"
#import "KnowMoreSCell.h"
#import "KnowMorePushView.h"
#import "KnowMoreStore.h"

#import "CYYServiceViewController.h"
#import "UINavigationBar+handle.h"

@interface KnowMoreVC () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) NSMutableArray *SubData;
@property (nonatomic, strong) KnowMorePushView *PushView;

@property (nonatomic, copy) NSString *searchString;

@end

@implementation KnowMoreVC
- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc]init];
        NSMutableArray *sub1 = [[NSMutableArray alloc] init];
        NSMutableArray *sub2 = [[NSMutableArray alloc] init];
        [_dataArr addObject:sub1];
        [_dataArr addObject:sub2];
    }
    return _dataArr;
}
- (NSMutableArray *)SubData{
    if (!_SubData) {
        _SubData = [[NSMutableArray alloc]init];
        NSMutableArray *sub1 = [[NSMutableArray alloc] init];
        NSMutableArray *sub2 = [[NSMutableArray alloc] init];
        [_SubData addObject:sub1];
        [_SubData addObject:sub2];
    }
    return _SubData;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self.navigationController.navigationBar navBarBackGroundColor:[UIColor colorWithHexString:@"#021b2f"] image:nil isOpaque:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initCSS];
    [self loadData];
}
- (void)initCSS{
    
    [self setNavBarWithTitle:@"了解更多"];
    
    ViewHeader *header = [[ViewHeader alloc]initWithKnowMore];
    [header setSearchBlock:^(NSString *string) {
        [self SearchWith:string];
    }];
    [self.view addSubview:header];
    [header mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(96);
        make.left.right.top.equalTo(self.view);
        
    }];
    
    UIButton *bottom = [[UIButton alloc]init];
    [bottom addTarget:self action:@selector(GoMessage) forControlEvents:UIControlEventTouchUpInside];
    bottom.layer.masksToBounds = YES;
    bottom.layer.cornerRadius = 22.5;
    bottom.layer.borderWidth = 0.5;
    bottom.layer.borderColor = [UIColor colorWithHexString:@"5FC192"].CGColor;
    bottom.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:18];
    [bottom setTitleColor:[UIColor colorWithHexString:@"5FC192"] forState:UIControlStateNormal];
    [bottom setTitle:@"在线客服" forState:UIControlStateNormal];
    [self.view addSubview:bottom];
    [bottom mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(33);
        make.right.equalTo(self.view.mas_right).offset(-33);
        make.bottom.equalTo(self.view.mas_bottom).offset(-22);
        make.height.mas_equalTo(45);
    }];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.top.equalTo(header.mas_bottom);
        make.right.equalTo(self.view.mas_right);
        make.bottom.equalTo(bottom.mas_top).offset(-30);
    }];
    self.PushView = [[KnowMorePushView alloc]init];
    [self.view addSubview:self.PushView];
}
- (void)SearchWith:(NSString *)string{
    self.dataArr = [[NSMutableArray alloc] initWithArray:self.SubData copyItems:YES];
    if ([string isEqualToString:@""]) {
        
    }else{
        NSMutableArray *arr1 = self.dataArr[0];
        NSMutableArray *arr2 = self.dataArr[1];
        NSMutableArray *sub1 = [[NSMutableArray alloc]init];
        NSMutableArray *sub2 = [[NSMutableArray alloc]init];
        if (arr1.count > 0) {
            for (KnowMoreModel *model in arr1) {
                if(([model.SubCont rangeOfString:string].location != NSNotFound) ||
                   ([model.Whydo rangeOfString:string].location != NSNotFound)){
                    [sub1 addObject:model];
                }
            }
            [self.dataArr replaceObjectAtIndex:0 withObject:sub1];
        }
        if (arr2.count > 0) {
            for (KnowMoreSecmodel *model in arr2) {
                if (model.problems.count > 0) {
                    for (NSDictionary *dic in model.problems) {
                        if (([model.group rangeOfString:string].location != NSNotFound) || ([dic[@"title"] rangeOfString:string].location != NSNotFound) || ([dic[@"content"] rangeOfString:string].location != NSNotFound)) {
                            [sub2 addObject:model];
                        }
                    }
                }else{
                    if ([model.group rangeOfString:string].location != NSNotFound) {
                        [sub2 addObject:model];
                    }
                }
            }
            for (NSInteger i = 0; i < sub2.count; i++) {
                id model = sub2[i];
                for (NSInteger j = i + 1; j < sub2.count; j++) {
                    id model2 = sub2[j];
                    if (model == model2) {
                        [sub2 removeObjectAtIndex:j];
                        j -= 1;
                    }
                }
            }
            [self.dataArr replaceObjectAtIndex:1 withObject:sub2];
        }
    }
    self.searchString = string;
    [self.tableView reloadData];
}
- (void)GoMessage{

    CYYServiceViewController *VC = [[CYYServiceViewController alloc]init];
    [self.navigationController pushViewController:VC animated:YES];
    
}
- (void)loadData{
    [[[KnowMoreStore alloc]init] GetProblemWithPrams:@{} Success:^(NSDictionary * _Nonnull res) {
        if ([res[@"msg"] isEqualToString:@"success"]) {
            [self GetDataWith:res[@"data"]];
        }else{
            [self showMBPError:res[@"msg"]];
        }
        [self.tableView reloadData];
    } Failure:^(NSError * _Nonnull error) {
        [self showMBPError:[HttpTool handleError:error]];
    }];
}
- (void)GetDataWith:(NSArray *)res{
    NSMutableArray *arr1 = self.dataArr[0];
    NSMutableArray *arr2 = self.dataArr[1];
    NSMutableArray *sub1 = self.SubData[0];
    NSMutableArray *sub2 = self.SubData[1];
    for (NSDictionary *dict1 in res) {
        if ([dict1[@"group"] isEqualToString:@"相关问题"]) {
            for (NSDictionary *dict2 in dict1[@"problems"]) {
                KnowMoreModel *model = [[KnowMoreModel alloc] init];
                model.CellPush = NO;
                model.SubCont = dict2[@"title"];
                model.Whydo = dict2[@"content"];
                [arr1 addObject:model];
                [sub1 addObject:model];
            }
        }else{
            KnowMoreSecmodel *model2 = [[KnowMoreSecmodel alloc] init];
            [model2 setValuesForKeysWithDictionary:dict1];
            model2.CellPush = NO;
            [arr2 addObject:model2];
            [sub2 addObject:model2];
        }
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataArr[section] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        NSArray *arr = self.dataArr[indexPath.section];
        if (arr.count > 0) {
            KnowMoreModel *model = arr[indexPath.row];
            if (model.CellPush == NO) {
                return 46;
            }else{
               return 46 + model.CellHeight;
            }
        }else{
            return 46;
        }
    }
    if (indexPath.section == 1) {
        NSArray *arr = self.dataArr[indexPath.section];
        if (arr.count > 0) {
            KnowMoreSecmodel *model = arr[indexPath.row];
            if (model.problems.count <= 2) {
                return 90;
            }else{
                if (model.CellPush == NO) {
                    return 90;
                }else{
                    return model.problems.count * 45;
                }
            }
        }else{
            return 46;
        }
    }
    return 90;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 43;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 43)];
    header.backgroundColor = [UIColor whiteColor];
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = [UIColor colorWithHexString:@"5FC192"];
    [header addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(header.mas_left).offset(14);
        make.centerY.equalTo(header.mas_centerY);
        make.width.mas_equalTo(2);
        make.height.mas_equalTo(14);
    }];
    UILabel *title = [[UILabel alloc]init];
    title.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:12];
    title.textColor = [UIColor colorWithHexString:@"666666"];
    if (section == 0) {
        title.text = @"相关问题";
    }
    if (section == 1) {
        title.text = @"更多问题";
    }
    [header addSubview:title];
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(line.mas_right).offset(3);
        make.centerY.equalTo(header.mas_centerY);
    }];
    UIView *lineView = [[UIView alloc]init];
    lineView.backgroundColor = [UIColor colorWithHexString:@"F5F5F5"];
    [header addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(header.mas_left);
        make.right.equalTo(header.mas_right);
        make.bottom.equalTo(header.mas_bottom);
        make.height.mas_equalTo(1);
    }];
    return header;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = nil;
    if (indexPath.section == 0) {
        [self.tableView registerNib:[UINib nibWithNibName:@"KnowMoreFCell" bundle:nil] forCellReuseIdentifier:@"KnowMoreFCellID"];
        KnowMoreFCell *FCell = [tableView dequeueReusableCellWithIdentifier:@"KnowMoreFCellID"];
        NSArray *arr = self.dataArr[indexPath.section];
        if (arr.count > 0) {
            KnowMoreModel *model = arr[indexPath.row];
            FCell.Search = self.searchString;
            FCell.Model = model;
            FCell.isPush = model.CellPush;
            [FCell setSendPushBlock:^(BOOL ISCellPush) {
                KnowMoreModel *ChangeModel = self.dataArr[indexPath.section][indexPath.row];
                ChangeModel.CellPush = ISCellPush;
                [tableView reloadData];
            }];
        }
        cell = FCell;
    }
    if (indexPath.section == 1) {
        [self.tableView registerNib:[UINib nibWithNibName:@"KnowMoreSCell" bundle:nil] forCellReuseIdentifier:@"KnowMoreSCellID"];
        KnowMoreSCell *SCell = [tableView dequeueReusableCellWithIdentifier:@"KnowMoreSCellID"];
        NSArray *arr = self.dataArr[indexPath.section];
        if (arr.count > 0) {
            SCell.Search = self.searchString;
            KnowMoreSecmodel *model = arr[indexPath.row];
            SCell.model = model;
            SCell.isPush = model.CellPush;
            [SCell setSendPushBlock:^(BOOL ISCellPush) {
                KnowMoreSecmodel *ChangeModel = self.dataArr[indexPath.section][indexPath.row];
                ChangeModel.CellPush = ISCellPush;
                [tableView reloadData];
            }];
            [SCell setViewPushBlock:^(NSInteger rowi) {
                self.PushView.conttitle = model.group;
                self.PushView.detail = model.problems[rowi][@"content"];
                [self.PushView ViewPush];
            }];
        }
        cell = SCell;
    }
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}





@end
